Thread: null an int[]

  1. #1
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101

    null an int[]

    Hi there, I have been writing some code to convert a number in base x to base y. During the operations, I would like to null the integer array so I can re-use it, so I don't have to create another integer array.

    Code:
    int a[20];
    /*give a some elements*/
    a = NULL /*not allowed*/
    Do I have to create a pointer to this array and use that the whole way through until it is time to null the pointer? Or is there a way do this? Forgive my ignorance, it seems like there should be an easy answer but I cannot find it by googling.

    Thanks

    ps: if you need my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define SIZE 20
    
    void toDecimal(int base, int num[], int len);
    int toBase(int base,  int num, int a[]);
    void reverseit(int a[],int len);
    int arraytonum(int a[], int len);
    
    int main(int argc, char *argv[]) {
    
      if(argc > 5 || argc < 3) {
        printf("Usage: base 2210 4 5 \n(2210 in base 4 to base 5)\n");
        exit(1);
      }
    
      char *num = argv[1];
      int number[SIZE];
      int anotherarray[SIZE];  /* i just want to null number[] and not use this */
      int length=0;
      while(*num) {
        number[length] = *num - '0';
        num++;
        length++;
      }
    
      int frombase = atoi(argv[2]);
      int tobase = atoi(argv[3]);
      toDecimal(frombase,number,length);
      int numero = arraytonum(number,length);
    
      /*number = NULL; (i wish to null it here) */
    
      int newlen = toBase(tobase,numero,anotherarray);
      reverseit(anotherarray, newlen);
    
      int j;
      for(j=0;j<newlen;j++) {
        printf("anotherarray[&#37;d]=%d\n", j, anotherarray[j]);
      }
    
      return 0;
    
    }
    
    void toDecimal(int base, int num[], int len) {
      int k;
      for(k=0;k<len;k++) {
        num[k] *= (int)pow(base,len-1-k);
      }
    }
    
    int toBase(int base, int num, int a[]) {
      int i=0;
      while( (num % base) != num ) {
        a[i] = num % base;
        num = (int)floor(num/base);
        i++;
      }
      a[i] = num;
    
      return i+1;
    }
    
    void reverseit(int a[], int len) {
      int i = 0;
      int temp;
      int loop = (int)floor(len/2);
      while(loop > 0) {
        temp = a[i];
        a[i] = a[len-1-i];
        a[len-1-i] = temp;
        loop--;
        i++;
      }
    }
    
    int arraytonum(int a[], int len) {
      int k;
      int num=0;
      for(k=0;k<len;k++) {
        num += a[k];
      }
    
      return num;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What does "null the integer array so I can re-use it" actually mean to you?

    The array is there, and you can't delete it, and setting it to NULL doesn't quite make sense in that context. Setting a pointer to NULL means it points to NULL. The array can't be changed where it points. In pointer terms, it's const in that regard.

    Are you just trying to set all elements of the array to zero? If so, use memset().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    The only good way is to write a function in the same style as all your other functions, which sets each element of the array to zero.

    A poor way (works in this specific case, but generally not so useful) is to use memset, eg
    memset( array, 0, sizeof array );

    Or you could put the array inside some scope, and re-initialise it that way
    Code:
    while ( something ) {
      int arr[SIZE] = { 0 };
      doStuff( arr );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you are using a static array of integers, I don't really see a need for you to use pointers here as you can just access what you want using the array index.

    An int cant be set to NULL. That is for pointers. Similarly '\0' is null for chars. For ints just reset it to 0.

  5. #5
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Okay, thanks for your replies guys, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM